//
//  CustomerSignatureController.m
//  Collection
//
//  Created by Neelam Shehzadi on 10/2/11.
//  Copyright 2011 Confiz Solution. All rights reserved.
//

#import "CustomerSignatureController.h"
#import "PreviewController.h"
#import "CarSectionTableViewController.h"
#import "imagePickViewController.h"


@implementation CustomerSignatureController
@synthesize isForDriverSignature_;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL success;
    drawImage = [[UIImageView alloc] initWithImage:nil];

    if(isForDriverSignature_)
    {
        self.navigationItem.title = @"Driver Sign";
        
        UIBarButtonItem *settingNavigationItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(buttonPreviewAction:)];
        [self.navigationItem setRightBarButtonItem:settingNavigationItem];
        [settingNavigationItem release];
        
       
        NSString *writableDBPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/driver.jpg"];
       
        success = [fileManager fileExistsAtPath:writableDBPath];
        if (success) 
            drawImage.image = [UIImage imageWithContentsOfFile:writableDBPath];
            

    }
    else
    {
        self.navigationItem.title = @"Customer Sign";
        
        UIBarButtonItem *settingNavigationItem = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonNextAction:)];
        settingNavigationItem.style = UIBarButtonItemStyleBordered;
        [self.navigationItem setRightBarButtonItem:settingNavigationItem];
        [settingNavigationItem release];
        
        NSString *writableDBPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/customer.jpg"];
        success = [fileManager fileExistsAtPath:writableDBPath];
        if (success)
            drawImage.image = [UIImage imageWithContentsOfFile:writableDBPath];


    }
    
    drawImage.frame = CGRectMake(0, 0, 320, 400);
    [self.view addSubview:drawImage];
    //    self.view.backgroundColor = [UIColor lightGrayColor];
    mouseMoved = 0;
    
    
    [self.view bringSubviewToFront:drawImage];
    [self.view bringSubviewToFront:btnSave];
    [self.view bringSubviewToFront:refreshBtn];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    // toolbar.hidden = YES;   
    
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    
    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }
    
    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;
    
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
    
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;
    
    
    UIGraphicsBeginImageContext(self.view.frame
                                .size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    lastPoint = currentPoint;
    
    mouseMoved++;
    
    if (mouseMoved == 10) {
        mouseMoved = 0;
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    
    if ([touch tapCount] == 2) {
        // toolbar.hidden = NO;
        drawImage.image = nil;
        return;
    }
    
    
    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

-(IBAction)buttonSaveImageAction{
    NSString  *pngPath;
    NSString  *jpgPath;
    
    if(!isForDriverSignature_)
    {
       pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/customer.png"];
       jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/customer.jpg"];
    }
    else
    {
        pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/driver.png"];
        jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/driver.jpg"];
    }
    
    // Write a UIImage to JPEG with minimum compression (best quality)
    // The value 'image' must be a UIImage object
    // The value '1.0' represents image compression quality as value from 0.0 to 1.0
    [UIImageJPEGRepresentation(drawImage.image, 1.0) writeToFile:jpgPath atomically:YES];
    
    // Write image to PNG
    [UIImagePNGRepresentation(drawImage.image) writeToFile:pngPath atomically:YES];
    
}
-(void)buttonClearAction:(id)sender
{
    drawImage.image = nil;
}

-(void)buttonPreviewAction:(id)sender
{
	/*CarSectionTableViewController *objCarSectionController = [[CarSectionTableViewController alloc] init];
    [self.navigationController pushViewController:objCarSectionController animated:YES];
    [objCarSectionController release];
    NSLog(@"preview action");*/
     [self buttonSaveImageAction];
    imagePickViewController *objCarSectionController = [[imagePickViewController alloc] init];
    [self.navigationController pushViewController:objCarSectionController animated:YES];
    [objCarSectionController release];
    NSLog(@"preview action");
   
}

-(void)buttonNextAction:(id)sender
{
    CustomerSignatureController *objCustomerSignatureController = [[CustomerSignatureController alloc] init];
    objCustomerSignatureController.isForDriverSignature_ = TRUE;
    [self.navigationController pushViewController:objCustomerSignatureController animated:YES];
    [objCustomerSignatureController release];
    [self buttonSaveImageAction];

	
    
}
@end
